package com.yc.common.utils;

import com.yc.common.core.domain.entity.SysUser;
import com.yc.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PasswordEncryptionUtil {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    // 这个方法只在需要时调用，用于加密已有用户的密码
    public void encryptExistingPasswords() {
        List<SysUser> users = userMapper.selectList(null);
        for (SysUser user : users) {
            // 只处理未加密的密码
            if (!user.getPassword().startsWith("$2a$")) {
                user.setPassword(passwordEncoder.encode(user.getPassword()));
                userMapper.updateById(user);
            }
        }
    }
}
